package com.bj58.groupbuy.action.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.bj58.dsap.api.scf.contract.IHbaseApi;
import com.bj58.dsap.api.scf.contract.Result;
import com.bj58.groupbuy.action.db.CommonDao;
import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.db.DBConnectionPool;
import com.bj58.groupbuy.action.db.JdbcUitl;
import com.bj58.groupbuy.action.model.ABTest;
import com.bj58.groupbuy.action.model.NewDisplayCate;
import com.bj58.groupbuy.action.model.SearchWordVo;
import com.bj58.groupbuy.action.model.TrafficUrlVo;
import com.bj58.groupbuy.action.service.DataReportService;
import com.bj58.groupbuy.action.service.NewDisplayCateService;
import com.bj58.groupbuy.action.util.DateUtil;
import com.bj58.groupbuy.action.util.ScfUtil;
import com.bj58.groupbuy.action.util.StringUtil;
import com.bj58.groupbuy.service.locator.ServiceLocator;

public class DataReportServiceImpl implements DataReportService {
	
	private CommonDao commonDao = new CommonDao();
	private NewDisplayCateService newDisplayCateService = ServiceLocator.get(NewDisplayCateService.class);	
	private static IHbaseApi hbase;
	private static String tablename="SearchWordCount";
	private static Map<String,String> finalMap=new HashMap<String, String>();
	static {
		ScfUtil.initSCF();
		hbase=ScfUtil.getHbaseService();
	}
	


	@Override
	public List<SearchWordVo> getSearchWordDataFromSql(String beginDate,
			String product) throws Exception {
		// TODO Auto-generated method stub
		if ("全部".equals(product)) {
			product = "all";
		}

		List<SearchWordVo> tList = new ArrayList<SearchWordVo>();
		String sql = "SELECT rank,word,search_times from t_common_netflow_pc_word WHERE type='day' AND business_name='"
				+ product
				+ "' AND stat_date='"
				+ beginDate
				+ "' ORDER BY search_times DESC";
		Connection con = DBConnectionPool.getConnection(DBConfig.db_131_data_bi.getIntType());
		Statement st = null;
		ResultSet rs = null;
		try {
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				SearchWordVo tVo = new SearchWordVo();
				tVo.setRank(rs.getString(1));
				tVo.setWord(rs.getString(2));// =""+rs.getString(1)+","+rs.getString(2)+",";
				tVo.setWordCount(rs.getString(3));
				tList.add(tVo);

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUitl.closeResultSet(rs); JdbcUitl.closeStatement(st); JdbcUitl.closeConnection(con);
		}
		return tList;
	}

	@Override
	public List<SearchWordVo> getSearchWordDataFromHbase(String beginDate,
			String product, String city1, String cate1, String cate2)
			throws Exception {
		/** 通过rowkey查询 **/
		String cate = cate2.equals("全部") ? cate1 : cate2;
		String dateString = beginDate.replaceAll("-", "");
		String rowkey ="";
		if((!city1.equals("全部"))&&cate1.equals("全部"))//某个城市业务线数据查询
		{
			rowkey = city1 + "_业务线" + product + "_" + dateString;
		}else {
			if(cate.equals("车辆买卖与服务"))
			      cate="业务线"+product+cate;
			rowkey = city1 + "_" + cate + "_" + dateString;
		}
	    
		List<SearchWordVo> tList = new ArrayList<SearchWordVo>();
		Result rs = hbase.queryByRowKey(tablename, rowkey);
		Map<String, Object> wordlist = rs.getScfMap();
		wordlist.remove("rowKey");
		List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(
				wordlist.entrySet());
		Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {
			@Override
			public int compare(Map.Entry<String, Object> firstMapEntry,
					Map.Entry<String, Object> secondMapEntry) {
				Integer t1 = Integer.parseInt(secondMapEntry.getValue()
						.toString());
				Integer t2 = Integer.parseInt(firstMapEntry.getValue()
						.toString());
				return t1.compareTo(t2);
			}
		});

		for (Integer i = 0; i < infoIds.size(); i++) {
			SearchWordVo tVo = new SearchWordVo();
			Integer tInteger = i+1;
			tVo.setRank(tInteger.toString());
			tVo.setWord(infoIds.get(i).getKey().replaceFirst("cf:", ""));// =""+rs.getString(1)+","+rs.getString(2)+",";
			tVo.setWordCount(infoIds.get(i).getValue().toString());
			tList.add(tVo);
			// System.out.println(infoIds.get(i));
		}

		// Result rs=hbase.queryByRowKey("ZhHomeHeatmapHBase", prefix+rowkey);
		// System.out.println(++count + ":" +
		// rs.getScfMap().get("rowKey")+rs.getScfMap().get("cf:pv"));
		return tList;

	}

	/**
	 * M端搜索词TOP 50 
	 * @author zhaoxiang
	 * @param 日期，业务线，一级城市，二级类
	 */
	@Override
	public List<SearchWordVo> getSearchWordDataFromHbase_M(String searchDate, String businessName, String city1, String cate1, String cate2,String type)
			throws Exception {
		/** 通过rowkey查询 **/
		String keyForms[] = {city1,businessName,cate1,cate2,searchDate,type};
		String rowkey =StringUtils.join(keyForms,"_");
	    
		List<SearchWordVo> tList = new ArrayList<SearchWordVo>();
		Result rs = hbase.queryByRowKey(tablename, rowkey);
		Map<String, Object> wordlist = rs.getScfMap();
		wordlist.remove("rowKey");
		List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(wordlist.entrySet());
		Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {
			@Override
			public int compare(Map.Entry<String, Object> firstMapEntry,Map.Entry<String, Object> secondMapEntry) {
				Integer t1 = Integer.parseInt(secondMapEntry.getValue().toString());
				Integer t2 = Integer.parseInt(firstMapEntry.getValue().toString());
				return t1.compareTo(t2);
			}
		});

		for (Integer i = 0; i < infoIds.size(); i++) {
			SearchWordVo tVo = new SearchWordVo();
			Integer tInteger = i+1;
			tVo.setRank(tInteger.toString());
			tVo.setWord(infoIds.get(i).getKey().replaceFirst("cf:", ""));
			tVo.setWordCount(infoIds.get(i).getValue().toString());
			tList.add(tVo);
		}
		return tList;

	}


	@Override
	public List<String> getCate1List(String ChannelName) throws Exception {
		// TODO Auto-generated method stub
		List<String> cate2List = new ArrayList<String>();
		List<NewDisplayCate> disCateListTwo =  new ArrayList<NewDisplayCate>();
		cate2List.add("全部");
		
			disCateListTwo = newDisplayCateService.getLevelOne(ChannelName, false);//1级
			for (int i = 0; i < disCateListTwo.size(); i++) {
				cate2List.add(disCateListTwo.get(i).getCate1_name());
			}
		
		
		return cate2List;
	}

	@Override
	public List<String> getCate2List(String ChannelName, String cate1)
			throws Exception {
		// TODO Auto-generated method stub
		List<String> cate2List = new ArrayList<String>();
		List<NewDisplayCate> disCateListTwo =  new ArrayList<NewDisplayCate>();
		cate2List.add("全部");
		
		disCateListTwo = newDisplayCateService.getLevelTwo(ChannelName,cate1, false);//2级
		for (int i = 0; i < disCateListTwo.size(); i++) {
			cate2List.add(disCateListTwo.get(i).getCate2_name());
		}
		
		
		return cate2List;
	}
    //原固定表查询
	@Override
	public List<TrafficUrlVo> getTrafUrlDataFromSql(String year,
			String conditions) throws Exception {
		StringBuffer sql=new StringBuffer();
		sql.append("select stat_date,platform,url,source,click_tag,sum(source_pv) as source_pv,");
		sql.append("case when SUM(source_pv)=0 then 0.0 else SUM(pv)/SUM(source_pv) END as source_ctr,");
		sql.append("sum(pv) as pv,sum(uv) as uv,sum(click) as click,");
		sql.append("case when SUM(pv)=0 then 0.0 else SUM(click)/SUM(pv) END  as click_ctr from t_common_specialflow_");
		sql.append(year);
		sql.append(" where ");
		sql.append(conditions);
		return commonDao.getAllColsByVo(sql.toString(), TrafficUrlVo.class, DBConfig.db_131_data_bi.getIntType());
	}
	//单表查询调用，按日期排序（选日期维度）
	public List<TrafficUrlVo> getTrafUrlDataForOneTable(String table,
			String conditions,boolean selectdate) throws Exception {
		StringBuffer sql=new StringBuffer();
		sql.append("select stat_date,platform,url,source,click_tag,sum(source_pv) as source_pv,");
		sql.append("case when SUM(source_pv)=0 then 0.0 else SUM(pv)/SUM(source_pv) END as source_ctr,");
		sql.append("sum(pv) as pv,sum(uv) as uv,sum(click) as click,");
		sql.append("case when SUM(pv)=0 then 0.0 else SUM(click)/SUM(pv) END  as click_ctr from ");
		sql.append(table);
		sql.append(" where ");
		sql.append(conditions);
		return commonDao.getAllColsByVo(sql.toString(), TrafficUrlVo.class, DBConfig.db_131_data_bi.getIntType());
	}
	
	
	@Override
	public List<TrafficUrlVo> getTrafUrlDataFromString(ArrayList<String> resultaArrayList) throws Exception {
		return commonDao.populateDataForString(resultaArrayList, TrafficUrlVo.class);
	}
	/**
	 * 1、不带日期维度
	 *   key(需分组的维度字段拼接) value(需计算的字段拼接)
	 * 2、带日期维度
	 *   建立页码-需查询表映射，如果查询单表直接调用单表查询，如果需查询多表，调用多个单表查询
	 * 3、对查询出的结果计算公式
	 *   对于每个key,value修改追加value
	 * 4、对于筛选出的结果，拆分key,value并展现到前台
	 * */
	@Override
	public Map<String,String> getTrafUrlDataForYear(String groupby,
			String conditions, String sumcols,String statdate) throws Exception {
		// TODO Auto-generated method stub
		/**1、获取查询的日期*/
		String[] stat=statdate.split(";");
		String startDate =stat[0].split(":")[1];//查询开始日期
		String endDate = stat[1].split(":")[1];//查询结束日期
		List<String> searchTableList = new ArrayList<String>();//需查询的表集合
		searchTableList = DateUtil.subDateByPatter(startDate, endDate, "yyyy");
		Map<String,String > searchConditionMap = new HashMap<String,String>();//各表查询条件
		List<String> tempSearchTableList = new ArrayList<String>();
		String subType="yyyy";
		String searchBeginDate = "";
		String searchEndDate = "";
		String newCon = "";
		for(String temp : searchTableList){
			//获得各表查询条件
			if("yyyyMM".equals(subType)){
				String month = temp;
				if(DateUtil.getStringDate(DateUtil.strToDate(startDate, "yyyy-MM-dd"), "yyyyMM").equals(month)){
					searchBeginDate = startDate;
				}else{
					searchBeginDate = month.substring(0,4)+"-"+month.substring(4)+"-01";
				}
				if(DateUtil.getStringDate(DateUtil.strToDate(endDate, "yyyy-MM-dd"), "yyyyMM").equals(month)){
					searchEndDate = DateUtil.getStringDate(DateUtil.getDateAfter(DateUtil.strToDate(endDate, "yyyy-MM-dd"),1), "yyyy-MM-dd");
				}else{
					searchEndDate = DateUtil.getStringDate(DateUtil.getDateAfterMonth(DateUtil.strToDate(month.substring(0,4)+"-"+month.substring(4)+"-01", "yyyy-MM-dd"), 1), "yyyy-MM-dd");
				}
				String datecon="stat_date<'"+searchEndDate+"' and stat_date>='"+searchBeginDate+"'";
				newCon = conditions.replace("wherefordate", datecon);
				//重拼完整表名,与查询条件放入MAP
				temp  = "t_common_specialflow_"+temp;
				tempSearchTableList.add(temp);
				searchConditionMap.put(temp, newCon);
			}else if("yyyy".equals(subType)){
				String year = temp;
				if(DateUtil.getStringDate(DateUtil.strToDate(startDate, "yyyy-MM-dd"), "yyyy").equals(year)){
					searchBeginDate = startDate;
				}else{
					searchBeginDate = year+"-01-01";
				}
				if(DateUtil.getStringDate(DateUtil.strToDate(endDate, "yyyy-MM-dd"), "yyyy").equals(year)){
					searchEndDate = endDate;
				}else{
					searchEndDate = year+"-12-31";
				}
				String datecon="stat_date<='"+searchEndDate+"' and stat_date>='"+searchBeginDate+"'";
				newCon = conditions.replace("wherefordate", datecon);
				//重拼完整表名,与查询条件放入MAP
				temp  = "t_common_specialflow_"+temp;
				tempSearchTableList.add(temp);
				searchConditionMap.put(temp, newCon);
			}			
		}
		searchTableList = tempSearchTableList;
		finalMap.clear();
		
		for(String temp : searchTableList){
			String cols=searchConditionMap.get(temp);
			getNoDataForYear(groupby, sumcols, cols, temp);	
		}		
		return finalMap;
	}
	/**
	 * 1、不带日期维度
	 *   key(需分组的维度字段拼接) value(需计算的字段拼接)
	 *   groupby中字段以，隔开
	 *   sumcols是需要计算sum的字段
	 * */
	public void getNoDataForYear(String groupby,
			String sumcols, String conditions,String tablename) throws Exception {
		//维度字段拼接 组合KEY，需计算的字段拼接组合为value
		Connection con = DBConnectionPool.getConnection(DBConfig.db_131_data_bi.getIntType());
		Statement st = con.createStatement();
		ResultSet rs = null;
		int len=sumcols.split(",").length;
		String group=groupby.replaceAll(",", ",'#',");
		String sql="";
		if("".equals(groupby))
		{
		  sql="SELECT 1,"+sumcols+" from "+tablename+" WHERE "+conditions;
		}else {
		  sql="SELECT CONCAT("+group+") ,"+sumcols+" from "+tablename+" WHERE "+conditions;
		}
		rs = st.executeQuery(sql);
		while(rs.next()){//将计算的字段和分组的字段分开，group by中的字段作为一个key，sum的字段合并作为一个value
			StringBuffer value  = new StringBuffer();
			String key=rs.getString(1);
			if(finalMap.containsKey(key))//如果已包含，则叠加sum
			{
				String[] olddata=finalMap.get(key).split("\\$");
				len=olddata.length;
				for (int i = 0; i < len; i++) {
					value.append(StringUtil.StrAddstr(olddata[i], String.valueOf(rs.getObject(i+2))));
					if(i+1<len)
				    	value.append("$");
				}
			}else {//不包含，则新增
				for(int i=0;i<len;i++)
				{
				    value.append(String.valueOf(rs.getObject(i+2)));
				    if(i+1<len)
				    	value.append("$");
				}
			}
			finalMap.put(key, value.toString());
		}
		
	}

	@Override
	public int getTrafUrlDataForCount(String conditions)
			throws Exception {
		// TODO Auto-generated method stub
		String sql="SELECT COUNT(1) from (select 1 from t_common_specialflow_2015 where "+conditions+")  as tabcc";
		int count = commonDao.countAll(sql, DBConfig.db_131_data_bi.getIntType());
		return count;
	}
   /**
    * 有日期维度时查询
    * */
	@Override
	public Map<Integer,List<TrafficUrlVo>> getHasDateForsql(String conditions,
			String statdate,String isdown) throws Exception {
		// TODO Auto-generated method stub	
		/**1、获取查询的日期*/
		String[] stat=statdate.split(";");
		String startDate =stat[0].split(":")[1];//查询开始日期
		String endDate = stat[1].split(":")[1];//查询结束日期
		int beginnum=Integer.parseInt(stat[2].split(":")[1]);
		List<String> searchTableList = new ArrayList<String>();//需查询的表集合
		searchTableList = DateUtil.subDateByPatter(startDate, endDate, "yyyy");
		Map<String,String > searchConditionMap = new HashMap<String,String>();//各表查询条件
		List<String> tempSearchTableList = new ArrayList<String>();
		List<TrafficUrlVo> resultlist=new ArrayList<TrafficUrlVo>();
		String subType="yyyy";
		String searchBeginDate = "";
		String searchEndDate = "";
		String newCon = "";
		for(String temp : searchTableList){
			//获得各表查询条件
			if("yyyyMM".equals(subType)){
				String month = temp;
				if(DateUtil.getStringDate(DateUtil.strToDate(startDate, "yyyy-MM-dd"), "yyyyMM").equals(month)){
					searchBeginDate = startDate;
				}else{
					searchBeginDate = month.substring(0,4)+"-"+month.substring(4)+"-01";
				}
				if(DateUtil.getStringDate(DateUtil.strToDate(endDate, "yyyy-MM-dd"), "yyyyMM").equals(month)){
					searchEndDate = DateUtil.getStringDate(DateUtil.getDateAfter(DateUtil.strToDate(endDate, "yyyy-MM-dd"),1), "yyyy-MM-dd");
				}else{
					searchEndDate = DateUtil.getStringDate(DateUtil.getDateAfterMonth(DateUtil.strToDate(month.substring(0,4)+"-"+month.substring(4)+"-01", "yyyy-MM-dd"), 1), "yyyy-MM-dd");
				}
				String datecon="stat_date<'"+searchEndDate+"' and stat_date>='"+searchBeginDate+"'";
				newCon = conditions.replace("wherefordate", datecon);
				//重拼完整表名,与查询条件放入MAP
				temp  = "t_common_specialflow_"+temp;
				tempSearchTableList.add(temp);
				searchConditionMap.put(temp, newCon);
			}else if("yyyy".equals(subType)){
				String year = temp;
				if(DateUtil.getStringDate(DateUtil.strToDate(startDate, "yyyy-MM-dd"), "yyyy").equals(year)){
					searchBeginDate = startDate;
				}else{
					searchBeginDate = year+"-01-01";
				}
				if(DateUtil.getStringDate(DateUtil.strToDate(endDate, "yyyy-MM-dd"), "yyyy").equals(year)){
					searchEndDate = endDate;
				}else{
					searchEndDate = year+"-12-31";
				}
				String datecon="stat_date<='"+searchEndDate+"' and stat_date>='"+searchBeginDate+"'";
				newCon = conditions.replace("wherefordate", datecon);
				//重拼完整表名,与查询条件放入MAP
				temp  = "t_common_specialflow_"+temp;
				tempSearchTableList.add(temp);
				searchConditionMap.put(temp, newCon);
			}			
		}
		searchTableList = tempSearchTableList;
		int count=0;
		for(String temp : searchTableList){
			String cols=searchConditionMap.get(temp);
			resultlist.addAll(getTrafUrlDataForOneTable(temp, cols,true));	
		}	
		if("".equals(isdown))
		{
			count=resultlist.size();
			//1、计算单表的数据量，小于最小值则直接略过，当count大于最小值的时候开始计数，当count小于最大值的时候停止计数，返回对应的值
			/*int tablecount=0;
			int addintable=0;
			int flag=0;
			for(String temp : searchTableList){
				String cols=searchConditionMap.get(temp);
				tablecount= commonDao.countAll("SELECT COUNT(1) from (select 1 from "+temp+" where "+cols+")  as tabcc", DBConfig.db_131_data_bi.getIntType());
				count +=tablecount;
				if(flag==0)
				{
					if(addintable>=20)
						flag=1;
					if(count>=beginnum&&count<endnum)
					{
					  cols=cols+" LIMIT "+(tablecount+beginnum-count)+","+(count-beginnum)+"";
					  addintable +=count-beginnum;
					  resultlist.addAll(getTrafUrlDataForOneTable(temp, cols,true));	
					}else if(count>=endnum){
						
					  cols=cols+" LIMIT "+beginnum+","+(20-addintable);
					  resultlist.addAll(getTrafUrlDataForOneTable(temp, cols,true));
					  flag=1;
					}
				}
			}	*/
			if(resultlist.size() > 20){
				int begin  = beginnum;
				List<TrafficUrlVo> limitList = new ArrayList<TrafficUrlVo>();
				for(int i = begin ; i < (begin+20); i++){
					limitList.add(resultlist.get(i));
					if((i+1) == resultlist.size()){
						break;
					}
				}
				resultlist = limitList;
			}
			
		}else {
			count=1;
		}
		Map<Integer,List<TrafficUrlVo>> rsMap=new HashMap<Integer, List<TrafficUrlVo>>();
		rsMap.put(count, resultlist);
		return rsMap;
	}

	/**abtest查询
	 */
	public List<ABTest> getABTestDataFromSql(String businessline,String pagetype,String platform,String condition)throws Exception
	{
		StringBuffer sql=new StringBuffer();
		sql.append("select id,businessline,pagetype,platform,versioncode,creater,createrTime,state,mark");
		sql.append(" from abtest");
	
		sql.append(" where businessline ='" +businessline+"'");
		sql.append(" and pagetype ='" +pagetype+"'");
		sql.append(" and platform ='" +platform+"'");
		sql.append(" order by createrTime DESC ");
		sql.append(" " +condition+" ");
		return commonDao.getAllColsByVo(sql.toString(), ABTest.class, DBConfig.db_68_bi_sys.getIntType());
	}
	
	/**abtest查询条数
	 */
	public int getABTestDataForCount(String businessline,String pagetype,String platform)throws Exception
	{
		StringBuffer sql=new StringBuffer();
		sql.append("select COUNT(1)");
		sql.append(" from abtest");
	
		sql.append(" where businessline ='" +businessline+"'");
		sql.append(" and pagetype ='" +pagetype+"'");
		sql.append(" and platform ='" +platform+"'");
		
		int count = commonDao.countAll(sql.toString(), DBConfig.db_68_bi_sys.getIntType());
		return count;		
	}

	/**abtest 插入数据
	 */
	public int insertABTestDataFromSql(String businessline,String pagetype,String platform,String creater,String mark)throws Exception{
		
		StringBuffer sql=new StringBuffer();
		sql.append("select id,businessline,pagetype,platform,versioncode,creater,createrTime,state,mark");
		sql.append(" from abtest");
	
		sql.append(" where businessline ='" +businessline+"'");
		sql.append(" and pagetype ='" +pagetype+"'");
		sql.append(" and platform ='" +platform+"'");
		sql.append(" order by createrTime DESC  ");
		List<ABTest>resultlist = commonDao.getAllColsByVo(sql.toString(), ABTest.class, DBConfig.db_68_bi_sys.getIntType());
		
		String versioncode="";
		if(!resultlist.isEmpty())//如果有业务线，页面类型，平台组合的versioncode
		{			
			ABTest vo=resultlist.get(0);
			versioncode=vo.getVersioncode();
			String num=versioncode.substring(versioncode.lastIndexOf('_')+1, versioncode.length());
			
			int n=Integer.parseInt(num);
			n++;
			
			num=Integer.toString(n); 
			//位数不满4位时 前面补0
			while(num.length()<4)
			{
				num="0"+num;
			}
			versioncode=versioncode.substring(0,versioncode.lastIndexOf('_')+1)+num;
			
		}else //没有组合的versioncode 从0001 开始
		{	
			versioncode=businessline+"_"+pagetype+"_"+platform+"_0001";
		}	
		
		System.out.println(versioncode);
			
		ABTest newvo=new ABTest();
		newvo.setBusinessline(businessline);
		newvo.setPagetype(pagetype);
		newvo.setPlatform(platform);
		newvo.setCreater(creater);
		newvo.setVersioncode(versioncode);
		newvo.setState(1);
		newvo.setCreaterTime(DateUtil.getStringDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
		newvo.setMark(mark);
			
		List<String> cols =new ArrayList<String>();
		cols.add("businessline");		
		cols.add("pagetype");
		cols.add("platform");
		cols.add("creater");
		cols.add("createrTime");
		cols.add("versioncode");
		cols.add("state");
		cols.add("mark");
		
		return commonDao.insertOneByVo(cols,"abtest",ABTest.class,newvo,DBConfig.db_68_bi_sys.getIntType());			
	}
	
	/**abtest 更新数据
	 */
	public boolean updateVo(ABTest vo) throws Exception {
		int res =  commonDao.updateByVo("abtest", "id=" + vo.getId(), ABTest.class, vo, DBConfig.db_68_bi_sys.getIntType());
		return res==0?false:true;
	}
	
	/**abtest 获得对象
	 */
	public ABTest getById(int id) throws Exception {
		String sql = "select * from abtest where id = " + id;
		return commonDao.getAllColsByVo(sql, ABTest.class, DBConfig.db_68_bi_sys.getIntType()).get(0);
	}
	
	/**abtest 查询 以创建者
	 */
	public List<ABTest> getABTestDataByCreater(String creater,String condition)throws Exception{
				
		StringBuffer sql=new StringBuffer();
		sql.append("select id,businessline,pagetype,platform,versioncode,creater,createrTime,state,mark");
		sql.append(" from abtest");
	
		sql.append(" where creater ='" +creater+"'");
		sql.append(" order by createrTime DESC ");
		sql.append(" " +condition+" ");
		return commonDao.getAllColsByVo(sql.toString(), ABTest.class, DBConfig.db_68_bi_sys.getIntType());
	}
	
	/**abtest 查询条数  以创建者
	 */
	public int getABTestCountByCreater(String creater)throws Exception{
		
		StringBuffer sql=new StringBuffer();
		sql.append("select COUNT(1)");
		sql.append(" from abtest");
		sql.append(" where creater ='" +creater+"'");
		
		int count = commonDao.countAll(sql.toString(), DBConfig.db_68_bi_sys.getIntType());
		return count;		
	}
}
